//
//  SEGCompleteGenderInfoViewController.m
//  ExcellentHome
//
//  Created by 梁昆 on 2017/11/2.
//  Copyright © 2017年 陈区. All rights reserved.
//
#import "SEGCompletePesonalInfoViewController.h"
#import "SEGCompleteGenderInfoViewController.h"

#import "SEGCompleteGenderInfoViewModel.h"

@interface SEGCompleteGenderInfoViewController ()<UIScrollViewDelegate> {
    BOOL isGenderSelected;
}
 
@property (nonatomic, strong) UIScrollView *aScrollView;
/*
 完善资料获得积分
 */
@property (nonatomic, strong)UILabel *completeInfoLabel;
/*
 让大家更懂你
 */
@property (nonatomic, strong)UILabel *promptLabel;
/*
 完善资料进度条
 */
@property (nonatomic, strong)UIView *progressBarView;
/*
 选择性别图标
 */
@property (nonatomic, strong)UIImageView *selectGenderImage;
/*
 分隔的小圆圈
 */
@property (nonatomic, strong)UIImageView *littleCircle;
/*
 分隔的小圆圈2
 */
@property (nonatomic, strong)UIImageView *littleCircle2;
/*
 性别
 */
@property (nonatomic, strong)UILabel *genderLabel;
/*
 个人资料
 */
@property (nonatomic, strong)UILabel *personalInfoLabel;
/*
 职业
 */
@property (nonatomic, strong)UILabel *JobLabel;
/*
 选择性别,获取积分文本
 */
@property (nonatomic, strong)UILabel *sexSelectionLabel;
/*
 男生头像
 */
@property (nonatomic, strong)UIButton *boyImageButton;
/*
 女生头像
 */
@property (nonatomic, strong)UIButton *girlImageButton;
/*
 选中图标
 */
@property (nonatomic, strong)UIImageView *selectImage;
/*
 下一步按钮
 */
@property (nonatomic, strong)UIButton *nextButton;
/*
 关闭按钮
 */
@property (nonatomic, strong)UIButton *closeButton;

/// viewModel
@property (nonatomic, strong) SEGCompleteGenderInfoViewModel *viewModel;

@end

@implementation SEGCompleteGenderInfoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 记录scrollview的contentsize的高度
    CGFloat boyImageMaxY = self.boyImageButton.frame.origin.y + self.boyImageButton.size.height;
    self.aScrollView.contentSize = CGSizeMake(LYScreenWidth(), boyImageMaxY + 10);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_W;
    isGenderSelected = NO;
    
    // 默认选值操作
    if ([self.viewModel.recordSex isEqualToString:@"1"]) { // 性别女
        [self girlImageButtonClick];
    } else if ([self.viewModel.recordSex isEqualToString:@"2"]) { // 性别男
        [self boyImageButtonClick];
    }
    
    [self seg_loadData];
    // 保存接口要提交的数据
    [self.viewModel seg_saveDataToUpdate];
    
    [self setupUI];
}

#pragma mark - Load Data
//请求获取积分
-(void)seg_loadData {
    @weakify(self);
    [self.viewModel.infoSignal subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.completeInfoLabel.text = [NSString stringWithFormat:@"完善资料可获得%@积分",self.viewModel.recordCoin];
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedFailureReason];
    }];
}

#pragma mark - UI
- (void)setupUI {
    self.aScrollView = [[UIScrollView alloc] init];
    self.aScrollView.showsHorizontalScrollIndicator = NO;
    self.aScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.nextButton];
    [self.view addSubview:self.aScrollView];
    [self.view addSubview:self.closeButton];
    [self.aScrollView addSubview:self.completeInfoLabel];
    [self.aScrollView addSubview:self.promptLabel];
    [self.aScrollView addSubview:self.progressBarView];
    [self.aScrollView addSubview:self.littleCircle];
    [self.aScrollView addSubview:self.littleCircle2];
    [self.aScrollView addSubview:self.selectGenderImage];
    [self.aScrollView addSubview:self.genderLabel];
    [self.aScrollView addSubview:self.personalInfoLabel];
    [self.aScrollView addSubview:self.JobLabel];
    [self.aScrollView addSubview:self.sexSelectionLabel];
    [self.aScrollView addSubview:self.boyImageButton];
    [self.aScrollView addSubview:self.girlImageButton];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_offset(15);
        make.right.equalTo(self.view).mas_offset(-15);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self.view).mas_offset(-30);
    }];
    
    [self.aScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.nextButton.mas_top);
    }];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).mas_offset(40);
        make.right.equalTo(self.view.mas_right).mas_offset(-20);
        make.width.height.mas_equalTo(17.5);
    }];
    
    [self.completeInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.aScrollView).mas_offset(85);
        make.centerX.equalTo(self.aScrollView);
    }];
    
    [self.promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.completeInfoLabel.mas_bottom).mas_offset(10);
        make.centerX.equalTo(self.aScrollView.mas_centerX);
    }];
    
    [self.progressBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.promptLabel.mas_bottom).mas_offset(40);
        make.centerX.equalTo(self.aScrollView.mas_centerX);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(1.5);
    }];
    
    [self.littleCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.progressBarView);
    }];
    
    [self.littleCircle2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.progressBarView.mas_right);
        make.centerY.equalTo(self.progressBarView.mas_centerY);;
    }];
    
    
    [self.selectGenderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.progressBarView.mas_left).mas_offset(-2);
        make.centerY.equalTo(self.progressBarView.mas_centerY);
        make.width.height.mas_equalTo(10);
    }];
    
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressBarView.mas_bottom).mas_offset(10);
        make.centerX.equalTo(self.selectGenderImage.mas_centerX);
    }];
    
    [self.personalInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressBarView.mas_bottom).mas_offset(10);
        make.centerX.equalTo(self.littleCircle.mas_centerX);
    }];
    
    [self.JobLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressBarView.mas_bottom).mas_offset(10);
        make.centerX.equalTo(self.littleCircle2.mas_centerX);
    }];
    
    [self.sexSelectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.personalInfoLabel.mas_bottom).mas_offset(85);
        make.centerX.equalTo(self.aScrollView.mas_centerX);
    }];
    
    [self.boyImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sexSelectionLabel.mas_bottom).mas_offset(40);
        make.right.equalTo(self.aScrollView.mas_centerX).mas_offset(-32.5);
        make.width.mas_equalTo(95);
        make.height.mas_equalTo(95);
    }];
    
    [self.girlImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sexSelectionLabel.mas_bottom).mas_offset(40);
        make.left.equalTo(self.aScrollView.mas_centerX).mas_offset(32.5);
        make.width.mas_equalTo(95);
        make.height.mas_equalTo(95);
    }];

}

#pragma mark - Event
//  女生头像点击
-(void)girlImageButtonClick {
    self.viewModel.recordSex = @"1";
    [self.viewModel seg_saveDataToUpdate];
    //头像已选中
    if (isGenderSelected == YES) {
        isGenderSelected = NO;
    }
    [self.girlImageButton addSubview:self.selectImage];
    isGenderSelected = YES;
    [self.selectImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.girlImageButton.mas_bottom);
        make.right.equalTo(self.girlImageButton.mas_right);
    }];
}
// 男生头像点击
- (void)boyImageButtonClick {
    self.viewModel.recordSex = @"2";
   [self.viewModel seg_saveDataToUpdate];
    //头像已选中
    if (isGenderSelected == YES) {
        isGenderSelected = NO;
    }
    [self.boyImageButton addSubview:self.selectImage];
    isGenderSelected = YES;
    [self.selectImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.boyImageButton.mas_bottom);
        make.right.equalTo(self.boyImageButton.mas_right);
    }];
}
// 下一步按钮
- (void)nextButtonClick {
    //跳转页面
    SEGCompletePesonalInfoViewController *personalInfoVC = [[SEGCompletePesonalInfoViewController alloc] init];
    personalInfoVC.recordCoin = self.viewModel.recordCoin;
    personalInfoVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:personalInfoVC animated:YES];
}
// 页面关闭按钮
- (void)closeButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Getter
// 完善资料获得积分
- (UILabel *)completeInfoLabel {
    if (!_completeInfoLabel) {
        _completeInfoLabel = [[UILabel alloc] init];
        if (self.viewModel.recordCoin.length == 0) {
            self.viewModel.recordCoin = @"0";
        }
        _completeInfoLabel.text = [NSString stringWithFormat:@"完善资料可获得%@积分",self.viewModel.recordCoin];
        _completeInfoLabel.font = [UIFont systemFontOfSize:18];
        _completeInfoLabel.textColor = kColor_T;
    }
    return _completeInfoLabel;
}
// 让大家更懂你
- (UILabel *)promptLabel {
    if (!_promptLabel) {
        _promptLabel = [[UILabel alloc] init];
        _promptLabel.text = @"让大家更懂你";
        _promptLabel.font = [UIFont systemFontOfSize:15];
        _promptLabel.textColor = kColor_T;
    }
    return _promptLabel;
}
// 完善资料进度条
- (UIView *)progressBarView {
    if (!_progressBarView) {
        _progressBarView = [[UIView alloc] init];
        _progressBarView.backgroundColor = kColor_L2;
        _progressBarView.layer.cornerRadius = 0.75;
        _progressBarView.layer.masksToBounds = YES;

    }
    return _progressBarView;
}
// 小圆圈
- (UIImageView *)littleCircle {
    if (!_littleCircle) {
        _littleCircle = [[UIImageView alloc] init];
        _littleCircle.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_dot")];
    }
    return _littleCircle;
}
// 小圆圈
- (UIImageView *)littleCircle2 {
    if (!_littleCircle2) {
        _littleCircle2 = [[UIImageView alloc] init];
        _littleCircle2.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_dot")];
    }
    return _littleCircle2;
}
// 选择性别图标
- (UIImageView *)selectGenderImage {
    if (!_selectGenderImage) {
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")];
        _selectGenderImage = [[UIImageView alloc] initWithImage:image];
    }
    return _selectGenderImage;
}
// 性别
- (UILabel *)genderLabel {
    if (!_genderLabel) {
        _genderLabel = [[UILabel alloc] init];
        _genderLabel.text = @"性别";
        _genderLabel.font = [UIFont systemFontOfSize:11];
        _genderLabel.textColor = kColor_T;
    }
    return _genderLabel;
}
// 个人资料
- (UILabel *)personalInfoLabel {
    if (!_personalInfoLabel) {
        _personalInfoLabel = [[UILabel alloc] init];
        _personalInfoLabel.text = @"个人资料";
        _personalInfoLabel.font = [UIFont systemFontOfSize:11];
        _personalInfoLabel.textColor = kColor_3;
    }
    return _personalInfoLabel;
}
// 职业
- (UILabel *)JobLabel {
    if (!_JobLabel) {
        _JobLabel = [[UILabel alloc] init];
        _JobLabel.text = @"职业";
        _JobLabel.font = [UIFont systemFontOfSize:11];
        _JobLabel.textColor = kColor_3;
    }
    return _JobLabel;
}
// 选择性别
- (UILabel *)sexSelectionLabel {
    if (!_sexSelectionLabel) {
        _sexSelectionLabel = [[UILabel alloc] init];
        _sexSelectionLabel.text = @"选择性别，获取积分";
        _sexSelectionLabel.font = [UIFont systemFontOfSize:16];
        _sexSelectionLabel.textColor = kColor_3;
    }
    return _sexSelectionLabel;
}
// 男生头像
- (UIButton *)boyImageButton {
    if (!_boyImageButton) {
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_logon_man")];
        _boyImageButton = [[UIButton alloc] init];
        [_boyImageButton setImage:image forState:UIControlStateNormal];
        [_boyImageButton setImage:image forState:UIControlStateHighlighted];
        [_boyImageButton addTarget:self action:@selector(boyImageButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _boyImageButton;
}
// 女生头像
- (UIButton *)girlImageButton {
    if (!_girlImageButton) {
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_logon_woman")];
        _girlImageButton = [[UIButton alloc] init];
        [_girlImageButton setImage:image forState:UIControlStateNormal];
        [_girlImageButton setImage:image forState:UIControlStateHighlighted];
        [_girlImageButton addTarget:self action:@selector(girlImageButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _girlImageButton;
}
// 选中图标
- (UIImageView *)selectImage {
    if (!_selectImage) {
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_login_select")];
        _selectImage = [[UIImageView alloc] initWithImage:image highlightedImage:image];
    }
    return _selectImage;
}
// 下一步按钮
- (UIButton *)nextButton {
    if (!_nextButton) {
        _nextButton = [[UIButton alloc] init];
        _nextButton.backgroundColor = kColor_T;
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextButton setTitleColor:kColor_W forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_nextButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [_nextButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
        [_nextButton.layer setMasksToBounds:YES];
        [_nextButton.layer setCornerRadius:2];
    }
    return _nextButton;
}
// 关闭按钮
- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [[UIButton alloc] init];
        [_closeButton setImage: [UIImage imageWithContentsOfFile:SEGPicName(@"btn_dialog_close_gray")] forState:UIControlStateNormal];
        _closeButton.backgroundColor = [UIColor clearColor];
        [_closeButton addTarget:self action:@selector(closeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}
- (SEGCompleteGenderInfoViewModel *)viewModel {
    if (!_viewModel) _viewModel = [SEGCompleteGenderInfoViewModel new];
    return _viewModel;
}

@end
